package com.js.lib_common.base;


import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.js.lib_common.R;
import com.js.lib_common.view.DialogHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.annotations.Nullable;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.observers.DisposableObserver;
import me.yokeyword.fragmentation.SupportFragment;


public abstract class BaseFragment extends SupportFragment {
    public Activity mActivity;
    protected Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutId(), container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;

    }

    protected abstract int getLayoutId();
    protected abstract void initView();
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mActivity = getActivity();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        setParallaxOffset(0.5f);
    }

    protected void initToolbarNav(Toolbar toolbar) {
        toolbar.setNavigationIcon(R.mipmap.ic_arrow_back_white_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                _mActivity.onBackPressed();
            }
        });
    }


    protected void initRecyclerView(RecyclerView rcyList, BaseQuickAdapter mAdapter,int spanCount ) {
        GridLayoutManager mGridLayoutManager = new GridLayoutManager(_mActivity, spanCount);
        //方向，水平
        mGridLayoutManager.setOrientation(RecyclerView.VERTICAL);
        //给RecyclerView设置布局管理器
        rcyList.setLayoutManager(mGridLayoutManager);
        rcyList.setHasFixedSize(true);
        rcyList.setLayoutManager(mGridLayoutManager);
        if (mAdapter!=null) {
            rcyList.setAdapter(mAdapter);
            mAdapter.openLoadAnimation();
            View view = LayoutInflater.from(_mActivity).inflate(R.layout.lib_rcy_emptyview, null);
            mAdapter.setEmptyView(view);
        }
    }

    protected void initSmartRefreshLayout(SmartRefreshLayout refreshLayout){
        refreshLayout.setRefreshHeader(new ClassicsHeader(_mActivity).setSpinnerStyle(SpinnerStyle.Translate));
        refreshLayout.setRefreshFooter(new ClassicsFooter(_mActivity).setSpinnerStyle(SpinnerStyle.Translate));

        refreshLayout.setEnableScrollContentWhenLoaded(true);//是否在加载完成时滚动列表显示新的内容
        refreshLayout.setEnableLoadMoreWhenContentNotFull(true);//是否在列表不满一页时候开启上拉加载功能
        refreshLayout.setEnableScrollContentWhenRefreshed(true);//是否在刷新完成时滚动列表显示新的内容 1.0.5
    }






    public void toastShow(int resId) {
        Toast.makeText(mActivity, resId, Toast.LENGTH_SHORT).show();
    }

    public void toastShow(String resId) {
        Toast.makeText(mActivity, resId, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mCompositeDisposable != null) {
            mCompositeDisposable.dispose();
        }
        if (unbinder != null) {
            unbinder.unbind();
        }

    }


    private CompositeDisposable mCompositeDisposable;


    public void addSubscription(DisposableObserver observer) {
        if (mCompositeDisposable == null) {
            mCompositeDisposable = new CompositeDisposable();
        }
        mCompositeDisposable.add(observer);
    }

    public Dialog loadingDialog;

    public Dialog showLoadingDialog() {
        if (loadingDialog==null)
        loadingDialog = new DialogHelper().getLoading(_mActivity, "加载中");
        if (!loadingDialog.isShowing())
        loadingDialog.show();
        return loadingDialog;
    }


    public Dialog showLoadingDialog(String message) {
        if (loadingDialog==null)
        loadingDialog = new DialogHelper().getLoading(_mActivity, message);
        if (!loadingDialog.isShowing())
        loadingDialog.show();
        return loadingDialog;
    }


    public void dismissLoadingDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            // progressDialog.hide();会导致android.view.WindowLeaked
            loadingDialog.dismiss();
        }
    }
}
